package com.jpcc.vw.onlineRadio.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.bean.RadioDeailBean;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.utils.ViewHolderUtils;
import com.jpcc.vw.onlineRadio.widget.Indicator;
import com.orhanobut.logger.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RadioScheduleAdapter extends BaseAdapter {

    private Context contex;
    private List<RadioDeailBean.RadioSchedule> datas = new ArrayList<>();

    public RadioScheduleAdapter(List<RadioDeailBean.RadioSchedule> sour, Context x) {
        if (sour != null) {
            datas.clear();
            datas.addAll(sour);
            contex = x;
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(contex, R.layout.adapter_album_radio_item, null);
        }
        initViews(position, convertView);
        return convertView;
    }


    private void initViews(int position, View convertView) {
        RadioDeailBean.RadioSchedule schedule = datas.get(position);
        TextView tv_item_count = ViewHolderUtils.get(convertView, R.id.tv_item_count);
        TextView tv_content2 = ViewHolderUtils.get(convertView, R.id.tv_content2);
//        Indicator imv_playing = ViewHolderUtils.get(convertView, R.id.imv_playing);
        RelativeLayout layout_top = ViewHolderUtils.get(convertView, R.id.layout_top);
        TextView tv_schedule = ViewHolderUtils.get(convertView, R.id.tv_schedule);
        TextView tv_schedule_day=ViewHolderUtils.get(convertView,R.id.tv_schedule_day);
        ImageView default_radio=ViewHolderUtils.get(convertView,R.id.imv_radio_cover);
        ImageView imv_play_status=ViewHolderUtils.get(convertView,R.id.imv_play_status);
//        default_radio.setVisibility(View.GONE);

        if(schedule.getRelatedProgram()!=null){
            Glide.with(contex).load(schedule.getRelatedProgram().getBackPicUrl()).into(default_radio);
        }




        tv_item_count.setText(position + 1 + "");
        tv_content2.setText(schedule.getRelatedProgram().getProgramName());
        if(schedule.isYestorday()){
            tv_schedule_day.setText(R.string.string_70);
        }
        else{
            tv_schedule_day.setText(R.string.string_69);
        }
//        if(TextUtils.isEmpty(schedule.getListenBackUrl())){
//            layout_top.setBackgroundColor(Color.parseColor("#BEBEBE"));
//        }
//        else{
//            layout_top.setBackgroundColor(Color.parseColor("#0e0e0e"));
//        }





        String bgColor = "#404040";
        if (Constants.isRelease) {
            try {
                boolean atmosphereStatus = AtmosphereLampManager.getInstance(contex).isHmiAtmosphereLampOn();
                String color = AtmosphereLampManager.getInstance(contex).getHmiAtmosphereLampColor();
                if (atmosphereStatus) {
                    if (!TextUtils.isEmpty(color)) {
                        bgColor = color;
                    }
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }












        if (AlbumRadioAudioPlayer.get().getPlayMusic() == null) {
//            imv_playing.setVisibility(View.GONE);
            imv_play_status.setVisibility(View.GONE);
            layout_top.setBackgroundColor(Color.parseColor("#000000"));
        } else {
            if (AlbumRadioAudioPlayer.get().getPlayMusic().getTrackId() == schedule.getId()&&position==AlbumRadioAudioPlayer.get().getRadioPosition()) {
//                imv_playing.setVisibility(View.VISIBLE);
                imv_play_status.setVisibility(View.VISIBLE);
//                layout_top.setBackgroundColor(Color.parseColor(bgColor));
                layout_top.setBackgroundColor(Color.parseColor("#000000"));
            } else {
//                imv_playing.setVisibility(View.GONE);
                imv_play_status.setVisibility(View.GONE);
                layout_top.setBackgroundColor(Color.parseColor("#000000"));
                if (TextUtils.isEmpty(schedule.getListenBackUrl())) {
                    if (AlbumRadioAudioPlayer.get().getPlayMusic().getTrackId() == schedule.getRelatedProgram().getId()) {
                        if(!schedule.isYestorday()){
                            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                            Date start=null;
                            Date end=null;
                            Date current=new Date();
                            Logger.i("current Time-->"+current);
                            try {
                                start=sdf.parse(schedule.getStartTime());
                                start.setYear(current.getYear());
                                start.setMonth(current.getMonth());
                                start.setDate(current.getDate());
                                end=sdf.parse(schedule.getEndTime());
                                end.setYear(current.getYear());
                                end.setMonth(current.getMonth());
                                end.setDate(current.getDate());
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            if(current.getTime()>start.getTime()&&current.getTime()<end.getTime()){
//                                imv_playing.setVisibility(View.VISIBLE);
                                imv_play_status.setVisibility(View.VISIBLE);
//                                layout_top.setBackgroundColor(Color.parseColor(bgColor));
                                layout_top.setBackgroundColor(Color.parseColor("#000000"));
                            }
                            else{
//                                imv_playing.setVisibility(View.GONE);
                                imv_play_status.setVisibility(View.GONE);
                                layout_top.setBackgroundColor(Color.parseColor("#000000"));
                            }
                        }
                        else{
//                            imv_playing.setVisibility(View.GONE);
                            imv_play_status.setVisibility(View.GONE);
                            layout_top.setBackgroundColor(Color.parseColor("#000000"));
                        }



//                        imv_playing.setVisibility(View.VISIBLE);
//                        layout_top.setBackgroundColor(Color.parseColor(bgColor));


                    } else {
//                        imv_playing.setVisibility(View.GONE);
                        imv_play_status.setVisibility(View.GONE);
                        layout_top.setBackgroundColor(Color.parseColor("#000000"));
                    }
                }
                else if(position==AlbumRadioAudioPlayer.get().getRadioPosition()&&(AlbumRadioAudioPlayer.get().getPlayMusic().getTrackId() == schedule.getId())){
//                    imv_playing.setVisibility(View.VISIBLE);
                    imv_play_status.setVisibility(View.VISIBLE);
//                    layout_top.setBackgroundColor(Color.parseColor(bgColor));
                    layout_top.setBackgroundColor(Color.parseColor("#000000"));
                }
            }
        }
        tv_schedule.setText(schedule.getStartTime() + "~" + schedule.getEndTime());

        if(AlbumRadioAudioPlayer.get().isPlaying()||AlbumRadioAudioPlayer.get().isPreparing()){
//            imv_playing.setIndicatorPause(true);
            imv_play_status.setImageResource(R.drawable.ic_play_in_list);
        }
        else{
//            imv_playing.setIndicatorPause(false);
            imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
        }



    }

    public void setDatas(List<RadioDeailBean.RadioSchedule> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }
}
